<?php
/**
 * 
 * @author web
 *
 */
class FileManage {
	
	public function _initialize() {
		
	}
	//创建文件夹
	public function createFolder($arr){
		$rndname = randomstr(8);
		if(empty($arr['foldername'])){
			$arr['foldername'] = $rndname;
		}
		
		$Upload_folder = M("Upload_folder");
		$Upload_folder->pid = $arr['pid'];
		$Upload_folder->foldername = $arr['foldername'];
		$Upload_folder->folderkey = $arr['folderkey'].$rndname.'/';
		$Upload_folder->addid = $arr['addid'];
		$Upload_folder->addtime = time();
		$Upload_folder->sort = $arr['sort'];
		$Upload_folder->for_site = $arr['for_site'];
		$Upload_folder->for_front = $arr['for_front'];
		
		$Upload_folder->add();
		
		
	}
	
	//重命名文件夹
	
	//删除文件夹
	
	//查看文件夹内容
	
	//上传文件
	
	//重命名文件
	
	//删除文件
	
	//裁剪文件
	
	//查看文件
	
	public function index() {
		vendor("Qiniu/rs");
		
		/*echo __FILE__ . '<br/>';
		echo $QINIU_SECRET_KEY;
		exit();*/
		
		Qiniu_SetKeys(C('QINIU_ACCESS_KEY'), C('QINIU_SECRET_KEY'));
		$putPolicy = new Qiniu_RS_PutPolicy(C('QINIU_BUCKET'));
		$putPolicy->callbackUrl = "http://127.0.0.1/dev/etwxs/v5/index.php?s=/Test/fileadd.html";
		$putPolicy->callbackBody = "title=$(x:title)";
		$upToken = $putPolicy->Token(null);
		
		$this->assign('token',$upToken);
		$this->assign('QINIU_DOMAIN',C('QINIU_DOMAIN'));
		$this->assign('QINIU_UP_HOST',C('QINIU_UP_HOST'));
		
		$this->display();
	}
	
	public function fileadd() {
		vendor("Qiniu/rs");
		
		$addid = 2;
		$title = $_POST["title"];
		$key = $_POST["key"];
		
		$Upload_file = M("Upload_file");
		$data['filetitle'] = $title;
		$data['filekey'] = $key;
		$data['addid'] = $addid;
		$data['addtime'] = time();
		$id = $Upload_file->add($data);
		
		$result = array(
			'status' => 1
		);
		
		echo json_encode($result);
	}
	
	/**
	* 文件列表
	**/
	public function filelist() {
		vendor("Qiniu/http");
		vendor("Qiniu/rs");
		vendor("Qiniu/rsf");
		
		$key = $this->_get("key","trim");
		
		$client = new Qiniu_MacHttpClient(null);
		$data = Qiniu_RSF_ListPrefix($client,C('QINIU_BUCKET'),$key,'',5);
		
		$result = array(
			'allNums' => 1
		);
		$result['rows'] = array();
		
		$i = 0;
		foreach ($data[0] as $key => $value) {
			$result['rows'][$i]['id'] = randomstr(6);
			$result['rows'][$i]['title'] = '';
			$result['rows'][$i]['href'] = $value["key"];
			
			$i++;
		}
		
		echo json_encode($result);
	}
	
	/**
	* 文件删除
	**/
	public function filedel() {
		$id = $this->_get("id","trim");

		$Upload_file = M("Upload_file");
		$where = "id=$id";
		$Upload_file->where($where)->delete();
		
		$result = array(
			'status' => 1
		);
		
		echo json_encode($result);
	}
	
	/**
	* 文件删除
	**/
	public function fileedit() {
		$addid = 2;
		$id = $this->_get("id","trim");
		$title = $this->_get("title","trim");
		
		$Upload_file = M("Upload_file");
		$data['filetitle'] = $title;
		$where = "id=$id AND addid=".$addid;
		$Upload_file->where($where)->save($data);
		
		$result = array(
			'status' => 1
		);
		
		echo json_encode($result);
	}
	
	/**
	* 文件夹列表
	**/
	public function folderlist() {
		$addid = 2;
		$rootid = 2;
		$folder = array();
		$folder[] = array(
			'id' => '2',
			'pId' => '0',
			'name' => 'web的文件夹',
			'key' => 'kzvnoo/',
			'open' => 1,
			'iconSkin' => 'myfolder'
		);
		
		$Upload_folder = M("Upload_folder");
		$where   = "addid in ($addid)";
		$list = $Upload_folder->where($where)->order('addtime DESC ')->select();
		
		$i = 1;
		foreach($list as $v){
			if($rootid == $v['pid']){
				$folder[$i]['id'] = $v['id'];
				$folder[$i]['pId'] = $v['pid'];
				$folder[$i]['name'] = $v['foldername'];
				$folder[$i]['key'] = $v['folderkey'];
				$folder[$i]['iconSkin'] = 'myfolder';
			}
			
			$i++;
		}
		
		
		$folder[] = array(
			'id' => '1',
			'pId' => '0',
			'name' => '公共文件夹',
			'key' => 'common/',
			'iconSkin' => 'myfolder'
		);
		
		
		echo json_encode($folder);
	}
	
	/**
	* 文件夹创建
	**/
	public function folderadd() {
		$addid = 2;
		$foldername = $this->_get("foldername","trim");
		$pid = $this->_get("pid","trim");
		$pkey = $this->_get("pkey","trim");
		$key = $pkey.randomstr(6);
		
		$Upload_folder = M("Upload_folder");
		$data['foldername'] = $foldername;
		$data['pid'] = $pid;
		$data['folderkey'] = $key;
		$data['addid'] = $addid;
		$data['addtime'] = time();
		$id = $Upload_folder->add($data);
		
		$result = array(
			'status' => 1,
			'id' => $id,
			'pId' => $pid,
			'foldername' => $foldername,
			'key' => $key
		);
		
		echo json_encode($result);
	}
	/**
	* 文件夹删除
	**/
	public function folderdel() {
		$addid = 2;
		$id = $this->_get("id","trim");
		
		$Upload_folder   = M("Upload_folder");
		$where   = "id in ($id) AND addid=".$addid;
		$res  = $Upload_folder->where($where)->delete();
		
		$result = array(
			'status' => 1
		);
		
		echo json_encode($result);
	}
	/**
	* 文件夹重命名
	**/
	public function folderedit() {
		$addid = 2;
		$id = $this->_get("id","trim");
		$foldername = $this->_get("foldername","trim");
		
		$Upload_folder = M("Upload_folder");
		$data['foldername'] = $foldername;
		$where = "id=$id AND addid=".$addid;
		$Upload_folder->where($where)->save($data);
		
		$result = array(
			'status' => 1
		);
		
		echo json_encode($result);	
	}
	
	/**
	* 裁剪图片
	**/
	public function croper() {
		$datasrc = $this->_get("datasrc","trim");
		
		$this->assign('datasrc',$datasrc);
		
		$this->display();
	}
	/**
	*  处理裁剪图片
	**/
	public function cropdeal() {
		
	}
	
}
